package com.alibaba.nacos.plugin.datasource.impl.kingbase8;

import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.datasource.constants.FieldConstant;
import com.alibaba.nacos.plugin.datasource.constants.TableConstant;
import com.alibaba.nacos.plugin.datasource.mapper.ConfigTagsRelationMapper;
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
import com.alibaba.nacos.plugin.datasource.model.MapperResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.alibaba.nacos.plugin.datasource.constants.DefaultConstant.DEFAULT_TENANT_ID;

public class ConfigTagsRelationMapperByKingbase8 extends AbstractKingbase8Mapper implements ConfigTagsRelationMapper {

    @Override
    public MapperResult findConfigInfo4PageCountRows(MapperContext context) {
        String appName = (String)context.getWhereParameter("appName");
        String dataId = (String)context.getWhereParameter("dataId");
        String group = (String)context.getWhereParameter("groupId");
        String tenantId = (String)context.getWhereParameter("tenant");
        String[] tagArr = (String[])((String[])context.getWhereParameter("tagARR"));
        List<Object> paramList = new ArrayList();
        StringBuilder where = new StringBuilder(" WHERE 1=1");
        final String sqlCount = "SELECT count(*) FROM config_info  a LEFT JOIN config_tags_relation b ON a.id=b.id";

        if (StringUtils.isNotBlank(tenantId)) {
            where.append(" AND a.tenant_id=? ");
            paramList.add(tenantId);
        }
        if (StringUtils.isNotBlank(dataId)) {
            where.append(" AND a.data_id=? ");
            paramList.add(dataId);
        }
        if (StringUtils.isNotBlank(group)) {
            where.append(" AND a.group_id=? ");
            paramList.add(group);
        }
        if (StringUtils.isNotBlank(appName)) {
            where.append(" AND a.app_name=? ");
            paramList.add(appName);
        }
        where.append(" AND b.tag_name IN (");
        for (int i = 0; i < tagArr.length; i++) {
            if (i != 0) {
                where.append(", ");
            }
            where.append('?');
            paramList.add(tagArr[i]);
        }
        where.append(") ");
        return new MapperResult(sqlCount + where, paramList);
    }

    @Override
    public MapperResult findConfigInfo4PageFetchRows(MapperContext context) {
        String appName = (String)context.getWhereParameter("appName");
        String dataId = (String)context.getWhereParameter("dataId");
        String group = (String)context.getWhereParameter("groupId");
        int startRow = context.getStartRow();
        int pageSize = context.getPageSize();

        String[] tagArr = (String[])((String[])context.getWhereParameter("tagARR"));
        String tenantId = (String)context.getWhereParameter("tenant");
        List<Object> paramList = new ArrayList();


        StringBuilder where = new StringBuilder(" WHERE 1=1 ");
        final String sql =
                "SELECT a.id,a.data_id,a.group_id,a.tenant_id,a.app_name,a.content FROM config_info  a LEFT JOIN "
                        + "config_tags_relation b ON a.id=b.id";

        if (StringUtils.isNotBlank(tenantId)) {
            where.append(" AND a.tenant_id=? ");
            paramList.add(tenantId);
        }
        if (StringUtils.isNotBlank(dataId)) {
            where.append(" AND a.data_id=? ");
            paramList.add(dataId);
        }
        if (StringUtils.isNotBlank(group)) {
            where.append(" AND a.group_id=? ");
            paramList.add(group);
        }
        if (StringUtils.isNotBlank(appName)) {
            where.append(" AND a.app_name=? ");
            paramList.add(appName);
        }

        where.append(" AND b.tag_name IN (");
        for (int i = 0; i < tagArr.length; i++) {
            if (i != 0) {
                where.append(", ");
            }
            where.append('?');
            paramList.add(tagArr[i]);
        }
        where.append(") ");
        return new MapperResult(getDatabaseDialect().getLimitPageSqlWithOffset(sql + where, startRow, pageSize),paramList);
    }

    @Override
        public MapperResult findConfigInfoLike4PageCountRows(MapperContext context) {
        String appName = (String)context.getWhereParameter("appName");
        String dataId = (String)context.getWhereParameter("dataId");
        String group = (String)context.getWhereParameter("groupId");
        String content = (String)context.getWhereParameter("content");
        String[] tagArr = (String[])((String[])context.getWhereParameter("tagARR"));
        String tenantId = (String)context.getWhereParameter("tenant");

        List<Object> paramList = new ArrayList();

        StringBuilder where = new StringBuilder(" WHERE 1=1 ");
        final String sqlCountRows = "SELECT count(*) FROM config_info  a LEFT JOIN config_tags_relation b ON a.id=b.id ";

        if (StringUtils.isNotBlank(tenantId)) {
            where.append(" AND a.tenant_id=? ");
            paramList.add(tenantId);
        }
        if (!StringUtils.isBlank(dataId)) {
            where.append(" AND a.data_id LIKE ? ");
            paramList.add(dataId);
        }
        if (!StringUtils.isBlank(group)) {
            where.append(" AND a.group_id LIKE ? ");
            paramList.add(group);
        }
        if (!StringUtils.isBlank(appName)) {
            where.append(" AND a.app_name = ? ");
            paramList.add(appName);
        }
        if (!StringUtils.isBlank(content)) {
            where.append(" AND a.content LIKE ? ");
            paramList.add(content);
        }

        where.append(" AND b.tag_name IN (");
        for (int i = 0; i < tagArr.length; i++) {
            if (i != 0) {
                where.append(", ");
            }
            where.append('?');
            paramList.add(tagArr[i]);
        }
        where.append(") ");
        return new MapperResult(sqlCountRows + where, paramList);
    }

    @Override
    public MapperResult findConfigInfoLike4PageFetchRows(MapperContext context) {
        String appName = (String)context.getWhereParameter(FieldConstant.APP_NAME);
        String dataId = (String)context.getWhereParameter(FieldConstant.DATA_ID);
        String group = (String)context.getWhereParameter(FieldConstant.GROUP_ID);
        String tenantId = (String)context.getWhereParameter(FieldConstant.TENANT_ID);
        String content = (String)context.getWhereParameter(FieldConstant.CONTENT);
        String[] tagArr = (String[])((String[])context.getWhereParameter(FieldConstant.TAG_ARR));
        int startRow = context.getStartRow();
        int pageSize = context.getPageSize();

        List<Object> paramList = new ArrayList();

        StringBuilder where = new StringBuilder(" WHERE 1=1 ");
        final String sqlFetchRows = "SELECT a.id,a.data_id,a.group_id,a.tenant_id,a.app_name,a.content "
                + "FROM config_info a LEFT JOIN config_tags_relation b ON a.id=b.id ";


        if (StringUtils.isNotBlank(tenantId)) {
            where.append(" AND a.tenant_id=? ");
            paramList.add(tenantId);
        }
        if (!StringUtils.isBlank(dataId)) {
            where.append(" AND a.data_id LIKE ? ");
            paramList.add(dataId);
        }
        if (!StringUtils.isBlank(group)) {
            where.append(" AND a.group_id LIKE ? ");
            paramList.add(group);
        }
        if (!StringUtils.isBlank(appName)) {
            where.append(" AND a.app_name = ? ");
            paramList.add(appName);
        }
        if (!StringUtils.isBlank(content)) {
            where.append(" AND a.content LIKE ? ");
            paramList.add(content);
        }

        where.append(" AND b.tag_name IN (");
        for (int i = 0; i < tagArr.length; i++) {
            if (i != 0) {
                where.append(", ");
            }
            where.append('?');
            paramList.add(tagArr[i]);
        }
        where.append(") ");
        return new MapperResult(getDatabaseDialect().getLimitPageSqlWithOffset(sqlFetchRows + where, startRow, pageSize), paramList);
    }

    @Override
    public String getTableName() {
        return TableConstant.CONFIG_TAGS_RELATION;
    }

    @Override
    public String getFunction(String s) {
        return s;
    }
}
